﻿using System;
using Unity;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Management;
using System.Runtime.InteropServices;
using System.Diagnostics;

using System.Collections;

using log4net;
using log4net.Config;
using System.Reflection;


namespace AutoRule
{

    class CObserver : Singleton<CObserver>
    {
        public void Init()
        {
            _starttime = DateTime.Now;
            Thread mythread = new Thread(PerformanceCounterFun);
            mythread.Start();
        }

        public String GetInfo()
        {
            String req = "";


            System.TimeSpan ND =  DateTime.Now - _starttime;

            req = String.Format("\"starttime\":\"{0}\",\"cpurate\":\"{1}\",\"span\":\"{2}\"", _starttime.ToString(), Math.Round(_cpuLoad, 2) + "%", ND.Hours);
            req = "{" + req + "}";
            return req;
        }

        private static void PerformanceCounterFun()
        {

            PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            while (true)
            {
                Thread.Sleep(1000); // wait for 1 second   
                _cpuLoad = pc.NextValue();
               // ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
               // log.Debug("CPU load = " + _cpuLoad + " %.");

            }
        }

        

       private  DateTime _starttime;
       private static float _cpuLoad;

    }
}
